import {
    post
} from "../../utils/request"

// pages/bbs/bbs.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        leftList: [],
        rightList: [],
        leftHeight: 0,
        rightHeight: 0,
        finish: false
    },
    pageNum: 1,
    jump(e) {
        // debugger
        console.log(e);
        let b = e.currentTarget.dataset.item
        console.log(b);

        wx.navigateTo({
            url: '../bbsDetail/bbsDetail?postsid=' + b
        })

    },
    // 点击跳进聊天室页面
    chatroom(e){
        console.log(e);
        let b = e.currentTarget.dataset.id
        console.log(b);

        wx.navigateTo({
            url: '../chatRoom/index?postsid=' + b
        })
        
    },
    initData() {
        if (this.data.finish) {
            return;
        }

        post("https://showme.myhope365.com/api/bbs/bbsPosts/open/list", {
            pageNum: this.pageNum,
            pageSize: 30
        }).then(res => {
            console.log(res.data);
            this.data.list = this.data.list.concat(res.data.rows);
            this.handleList()
            if ((this.pageNum - 1) * 15 >= res.data.total) {
                //数据加载完成
                console.log(this.pageNum)
                this.data.finish = true
            }

            this.setData({
                finish: this.data.finish
            })
        })

    },


    handleList(){
        if(this.data.list.length === 0 ){
          return;
        }
        const item = this.data.list.shift();
    
        wx.getImageInfo({
          src: item.coverImgUrl,
          success:(res)=>{
            if(this.data.leftHeight <= this.data.rightHeight){
              this.data.leftList.push(item)
              this.data.leftHeight += res.height / res.width;
            
          }else {
            this.data.rightList.push(item)
            this.data.rightHeight += res.height / res.width;
          }
          },
          complete:()=> {
            this.handleList()
          }
        })
    
        this.setData({
          leftList: this.data.leftList,
          rightList: this.data.rightList
        })
    
      },



    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        post("https://showme.myhope365.com/api/bbs/bbsPosts/open/list", {
            pageNum: 1,
            pageSize: 30
        }).then(res => {
            console.log(res);
            this.setData({
                list: res.data.rows
            })
            console.log(this.data.list);
            this.handleList()
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.pageNum++
        this.initData()
    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})